package strcy.cn.market.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import strcy.cn.market.pojo.Product;
import strcy.cn.market.pojo.VO.ProductVO;
import strcy.cn.market.service.ProductService;
import strcy.cn.market.utils.PageBean;
import strcy.cn.market.utils.Result;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    //添加商品
    @PostMapping("/add")
    public Result addProduct(@RequestBody Product product) {
        productService.add(product);
        return Result.success();
    }
    //删除商品
    @DeleteMapping("{product_id}")
    public Result deleteProduct(@PathVariable("product_id") Integer id) {
        productService.deleteById(id);
        return Result.success();
    }
    //修改商品
    @PutMapping("/update")
    public Result updateProduct(@RequestBody Product product) {
        productService.update(product);
        return Result.success();
    }
    //获取商品列表
    @GetMapping("/list")
    public Result<List<ProductVO>> getListProduct() {
        List<ProductVO> list = productService.list();
        return Result.success(list);
    }
    //更改商品状态上下状态
    @PutMapping("/updateStatus")
    public Result addProduct(String Status ,Integer id) {
        productService.updateStatus(Status,id);
        return Result.success();
    }
    //商品分页查询
    @GetMapping("/pageList")
    public Result<PageBean<ProductVO>> getPageList(Integer pageNum, Integer pageSize){
        PageBean<ProductVO> list = productService.pageList(pageNum,pageSize);
        return Result.success(list);
    }
    //获取商品详情
    @GetMapping("{productId}")
    public Result<ProductVO> getProduct(@PathVariable("productId") Integer productId) {
        ProductVO p = productService.getById(productId);
        return Result.success(p);
    }
}
